<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
			<el-form-item label="报警日期">
				<el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-"
				 start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker>
			</el-form-item>
			<el-form-item label="报警类型" prop="warningType">
				<el-select v-model="queryParams.warningType" placeholder="请选择报警类型" clearable>
					<el-option v-for="dict in dict.type.warning_type" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-table v-loading="loading" :data="elecWarningList">
			<el-table-column label="主键ID" align="center" prop="warningId" />
			<el-table-column label="报警日期" align="center" prop="warningTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.warningTime, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="报警类型" align="center" prop="warningType">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.warning_type" :value="scope.row.warningType" />
				</template>
			</el-table-column>
			<el-table-column label="报警内容" align="center" prop="warningContent" />
			<el-table-column label="备注" align="center" prop="remark" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-search" @click="handleUpdate(scope.row)" v-hasPermi="['elec:elecWarningGc:see']">查看详情</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
		 @pagination="getList" />
		 
		 <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
		 	<el-table v-loading="loading" :data="elecMaterialList" height="450">
		 		<el-table-column label="主键ID" align="center" prop="materialId" />
		 		<el-table-column label="工器具编码" align="center" prop="materialCode" width="100" />
		 		<el-table-column label="工器具名称" align="center" prop="materialName" width="200" />
		 		<el-table-column label="分类" align="center" prop="sortName" width="100" />
		 		<el-table-column label="库房" align="center" prop="warehouseName" width="100" />
		 		<el-table-column label="柜架" align="center" prop="cabinetName" width="100" />
		 		<el-table-column label="层位" align="center" prop="gridName" width="100" />
		 		<el-table-column label="供应商" align="center" prop="providerName" width="100" />
		 	</el-table>
		 	<pagination v-show="totalMaterial>0" :total="totalMaterial" :page.sync="queryMaterialParams.pageNum" :limit.sync="queryMaterialParams.pageSize"
		 	 @pagination="getDetailsList" />
		 	<div slot="footer" class="dialog-footer">
		 		<el-button @click="cancel">取 消</el-button>
		 	</div>
		 </el-dialog>
	</div>
</template>

<script>
	import {
		listElecWarning,
		getElecWarning,
		delElecWarning,
		addElecWarning,
		updateElecWarning,
		listElecMaterialWarningDetails
	} from "@/api/elec/elecWarning";

	export default {
		name: "ElecWarning",
		dicts: ['warning_type'],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				//日期控件
				dateRange: [],
				// 总条数
				total: 0,
				// 选择工器具列表总条数
				totalMaterial: 0,
				// 工器具报警表格数据
				elecWarningList: [],
				// 工器具表格数据
				elecMaterialList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					warningTime: null,
					warningType: null,
					warningContent: null,
					materialCategory: "6",
				},
				queryMaterialParams: {
					pageNum: 1,
					pageSize: 10,
					warningId: null,
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {},
				pickerOptions: {
					shortcuts: [{
						text: '最近一周',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
							picker.$emit('pick', [start, end]);
						}
					}, {
						text: '最近一个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
							picker.$emit('pick', [start, end]);
						}
					}, {
						text: '最近三个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
							picker.$emit('pick', [start, end]);
						}
					}]
				},
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询工器具报警列表 */
			getList() {
				this.loading = true;
				listElecWarning(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
					this.elecWarningList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			getDetailsList(){
				this.loading = true;
				this.queryMaterialParams.warningId = this.form.warningId;
				listElecMaterialWarningDetails(this.queryMaterialParams).then(response => {
					console.log(response.rows)
				  this.elecMaterialList = response.rows;
				  this.totalMaterial = response.total;
				  this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					warningId: null,
					warningTime: null,
					warningType: null,
					warningContent: null,
					materialCategory: null,
					remark: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.dateRange = [];
				this.resetForm("queryForm");
				this.handleQuery();
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const warningId = row.warningId || this.ids
				getElecWarning(warningId).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "查看报警工具详情";
					
					this.getDetailsList();
				});
			},
		}
	};
</script>
